package com.example.mtblog.dto;


import com.example.mtblog.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
@NoArgsConstructor
@JsonIgnoreProperties({"enabled","accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities"})
public class LoginUser implements UserDetails {
    @Serial
    private static final long serialVersionUID = 1L;

    private  String UUID;
    private String loginTime; //登录时间
    private String expireTime;//过期时间
    private User user;//用户信息
    private LoginInfo loginInfo; //用户登录的信息

    public LoginUser(User user){
       this.user = user;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();


        authorities.add(new SimpleGrantedAuthority("user"));

        if(this.user.getRole().equals("admin"))
            authorities.add(new SimpleGrantedAuthority("admin"));
        return authorities;
    }

    public String getRole() {
        return this.user.getRole();
    }

    public Integer getUid() {
        return this.user.getUid();
    }

    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    public void setPassword(String newPassword) {
        this.user.setPassword(newPassword);
    }

    @Override
    public String getUsername() {
        return this.user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }



}
